﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NuiBlue.Bluetooth;

namespace NavigationExample
{
    public partial class Form1 : Form
    {
        public delegate void UpdateList(object sender, BluetoothEventArgs e);
        public delegate void Status(object sender, string status);

        BluetoothController bc;

        public Form1()
        {
            InitializeComponent();

            bc = new BluetoothController();
            bc.navigated += new BluetoothController.FolderHandler(bc_navigated);
            bc.StatusUpdate += new BluetoothController.StatusEventHandler(bc_StatusUpdate);
        }

        void statusUpdate(object sender, string status)
        {
            MessageBox.Show(status);
        }

        void bc_StatusUpdate(object sender, string status)
        {
            BeginInvoke(new Status(statusUpdate), sender, status);
        }

        void navigated(object sender, BluetoothEventArgs e)
        {
            lstFolders.Items.Clear();

            foreach (string folder in e.folders)
            {
                lstFolders.Items.Add(folder);
            }

            foreach (string file in e.files)
            {
                lstFolders.Items.Add(file);
            }
        }

        void bc_navigated(object sender, BluetoothEventArgs e)
        {
            lstFolders.BeginInvoke(new UpdateList(navigated), sender, e);
        }

        private void btnGet_Click(object sender, EventArgs e)
        {
            bc.setAddress(txtAddress.Text);
            bc.setPin(txtPin.Text);
            bc.getFolders(true);
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            bc.setAddress(txtAddress.Text);
            bc.setPin(txtPin.Text);
            bc.selectFolder(true, lstFolders.SelectedItem.ToString());
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            bc.setAddress(txtAddress.Text);
            bc.setPin(txtPin.Text);
            bc.folderUp(true);
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            bc.setAddress(txtAddress.Text);
            bc.setPin(txtPin.Text);
            bc.resetFolder(true);
        }
    }
}
